home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / xtoa.c < prev    next >
C/C++ Source or Header  |  1985-12-29  |  2KB  |  40 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ xtoa - convert an integer to hex representation.   */
  4. /*@        Very useful to prevent loading of printf    */
  5. /*@        and the 2-8K of follow-ons.  Since the hex  */
  6. /*@        value may contain a 0x00, only one integer  */
  7. /*@        is converted at a time (two bytes out).     */
  8. /*@        If you wish char output, then only use the  */
  9. /*@        second byte of the result.                  */
  10. /*@                                                    */
  11. /*@   Usage:     xtoa(num, buffer);                    */
  12. /*@       where num is an integer or character.        */
  13. /*@             buffer is a char area of at least      */
  14. /*@                three bytes.                        */
  15. /*@                                                    */
  16. /*@   Returns a pointer to the buffer.  This allows    */
  17. /*@       nesting of puts(xtoa(n, buffer)); variety.   */
  18. /*@                                                    */
  19. /*@*****************************************************/
  20.  
  21. char *xtoa(xval, aval)    /* convert 2 hex bytes to ascii  */
  22. int xval;
  23. char *aval;
  24. {
  25.     int i, temp;
  26.     char *save;
  27.  
  28.     save = aval;
  29.     for (i=3; i >= 0; i--) {
  30.         temp = (xval >> (i*4)) & 0x0f; /* get next nibble */
  31.         if (temp > 9)
  32.             *aval++ = temp + ('A' - 10); /* A through F */
  33.         else
  34.             *aval++ = temp + '0'; /* 0 through 9 */
  35.     }
  36.     *aval = '\0';    /* add end of string */
  37.  
  38.     return save;
  39. }
  40.